package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.ReportMapper;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.ReportService;
import com.itheima.health.service.UserService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.vo.RoleVO;
import com.itheima.health.vo.UserVO;
import lombok.SneakyThrows;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ReportServiceImpl  implements ReportService {

//    @Reference
//    private MemberService memberService;

    @Autowired
    private  ReportMapper reportMapper;


    @Override
    public List<Map> getSetmealProportion() {
        return reportMapper.getSetmealProportion();
    }

    private Map changeData(List<Map> mapList) {
         List<String> yearAndMonthList = new ArrayList<String>();
         List<Long> memberCountList = new ArrayList<Long>();
        Map map = new HashMap();
        for (Map map1 : mapList) {
            yearAndMonthList.add((String)map1.get("yearAndMonth"));
            memberCountList.add((Long)map1.get("memberCounts"));
        }
        map.put("memberCounts",memberCountList);
        map.put("yearAndMonth",yearAndMonthList);
        return map;
    }
    //根据月份查询会员数量
    @Override
    public Map findMemberCountsByMonth(String begin,String end) {
        List<Map> mapList = reportMapper.findMemberCountsByMonth(begin,end);
        Map map = changeData(mapList);
        return map;
    }


    @Override
    public List<Map> queryTheRatioBySex() {
        List<Map> mapList=new ArrayList<>();
        //男性会员
        Map manCount=reportMapper.findManCount();
        manCount.put("name","男");
        //女性会员
        Map womanCount=reportMapper.findWomanCount();
        womanCount.put("name",'女');
        mapList.add(manCount);
        mapList.add(womanCount);
        return mapList;
    }

    @Override
    public List<Map> QueryTheProportionAccordingToAge() {
        List<Map> mapList=new ArrayList<>();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //拿到当前时间字符串 yyyy-MM-dd
        String currentTime = simpleDateFormat.format(date);
        //截取 yyyy 与  -MM-dd
        String substring = currentTime.substring(0, 4);
        String yr = currentTime.substring(4, 10);
        //得到第一个年龄段所对应的日期字符串 18岁以内
        int subInt = (Integer.parseInt(substring))-18;
        String zeroToEighteen = subInt + yr;
        //得到第二个年龄段所对应的日期字符串 18岁到34岁
        int st = (Integer.parseInt(substring)) - 18;
        int ed = (Integer.parseInt(substring)) - 34;
        String startSeventeen = ed + yr;
        String endOfThirtyFour = st + yr;
        //得到第三个年龄段所对应的日期字符串 35岁到45岁
        int stt=(Integer.parseInt(substring))-45;
        int edd=(Integer.parseInt(substring))-35;
        String startFortyFive = stt + yr;
        String endThirtyFive= edd + yr;
        //得到第三个年龄段所对应的日期字符串 大于45岁
        int fortyFive = (Integer.parseInt(substring)) - 45;
        String overFortyFive = fortyFive + yr;

        //小于18
        Map oneToEighteen=reportMapper.findOneToEighteen(currentTime,zeroToEighteen);
        oneToEighteen.put("name","小于18岁");
        //18-34
        Map nineteenToThirtyFive=reportMapper.findNineteenToThirtyFive(startSeventeen,endOfThirtyFour);
        nineteenToThirtyFive.put("name","18岁到34岁");
        //35-45
        Map thirtySixToFortyFive=reportMapper.findThirtySixToFortyFive(startFortyFive,endThirtyFive);
        thirtySixToFortyFive.put("name","35岁到45岁");
        //45+
        Map greaterThanFortyFive=reportMapper.findGreaterThanFortyFive(overFortyFive);
        greaterThanFortyFive.put("name","大于45岁");
        mapList.add(oneToEighteen);
        mapList.add(nineteenToThirtyFive);
        mapList.add(thirtySixToFortyFive);
        mapList.add(greaterThanFortyFive);
        return mapList;
    }


    @Override
    public Map operatingStatistics() {
        Map map=new HashMap<>();
        //报告日期
        String nowDate = DateUtils.parseDate2String(DateUtils.getToday(), "yyyy-MM-dd");
        //总会员数
        int totalMember=reportMapper.findTotalMember();
        //TODO 当日数据
        //今日新增会员
        int todayNewMember=reportMapper.findTodayNewMember(nowDate);
        //今日预约数
        int todayOrderNumber=reportMapper.findTodayOrderNumber(nowDate);
        //今日到诊数
        int todayVisitsNumber=reportMapper.findTodayVisitsNumber(nowDate);
        //TODO 本周数据
        //获取当前日期的周一日期
        String ThisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday(), "yyyy-MM-dd");
        //获取当前日期的周末日期
        String SundayOfThisWeek = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek(), "yyyy-MM-dd");
        //本周新增会员
        int thisWeekNewMember=reportMapper.findThisWeekNewMember(ThisWeekMonday,SundayOfThisWeek);
        //本周预约数量
        int thisWeekOrderNumber=reportMapper.findThisWeekOrderNumber(ThisWeekMonday,SundayOfThisWeek);
        //本周到诊数量
        int thisWeekVisitsNumber=reportMapper.findThisWeekVisitsNumber(ThisWeekMonday,SundayOfThisWeek);
        //TODO 本月数据
        String firstDayMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4CurrentMonth(), "yyyy-MM-dd");
        String endDayMonth = DateUtils.parseDate2String(DateUtils.getLastDayOfMonth(), "yyyy-MM-dd");
        //本月新增会员
        int thisMonthNewMember=reportMapper.findThisMonthNewMember(firstDayMonth,endDayMonth);
        //本月预约数
        int thisMonthOrderNumber=reportMapper.findThisMonthOrderNumber(firstDayMonth,endDayMonth);
        //本月到诊数
        int thisMonthVisitsNumber=reportMapper.findThisMonthVisitsNumber(firstDayMonth,endDayMonth);
        //TODO 热门套餐
        //热门套餐降序排序
        List<Map> hotSetmeal=reportMapper.findHotSetmeal();
        //套餐预约总数
        int setmeal_total =reportMapper.findSetmealcount();
        double setmeal_total1 = setmeal_total;
        double proportion = 0;
        for (Map map1 : hotSetmeal) {
            long setmeal_count = (Long) map1.get("setmeal_count");
            proportion = 100 * setmeal_count * 1.0 / setmeal_total1;
            map1.put("proportion", String.format("%.2f", proportion) + "%");
        }
        List<Map> list = new ArrayList<>();
        for (int i = 0; i < hotSetmeal.size(); i++) {
            if (i < setmeal_total) {
                list.add(hotSetmeal.get(i));
            }
        }
        map.put("reportDate",nowDate);
        map.put("totalMember",totalMember);
        map.put("todayNewMember",todayNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",list);
        return map;
    }

    @SneakyThrows
    @Override
    public void exportExcels(Map map) {
        //工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("运营数据统计");
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("会员数据统计");

        XSSFRow row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("今日新增会员数");
        row1.createCell(1).setCellValue(String.valueOf(map.get("todayNewMember")));
        row1.createCell(2).setCellValue("总会员数");
        row1.createCell(3).setCellValue(String.valueOf(map.get("totalMember")));

        XSSFRow row2 = sheet.createRow(2);
        row2.createCell(0).setCellValue("本周新增会员数");
        row2.createCell(1).setCellValue(String.valueOf(map.get("thisWeekNewMember")));
        row2.createCell(2).setCellValue("本月新增会员数");
        row2.createCell(3).setCellValue(String.valueOf(map.get("thisMonthNewMember")));


        XSSFRow row3 = sheet.createRow(4);
        row3.createCell(0).setCellValue("预约到诊数据统计");
        XSSFRow row4 = sheet.createRow(5);
        row4.createCell(0).setCellValue("今日预约数");
        row4.createCell(1).setCellValue(String.valueOf(map.get("todayOrderNumber")));
        row4.createCell(2).setCellValue("今日到诊数");
        row4.createCell(3).setCellValue(String.valueOf(map.get("todayVisitsNumber")));

        XSSFRow row5 = sheet.createRow(6);
        row5.createCell(0).setCellValue("本周预约数");
        row5.createCell(1).setCellValue(String.valueOf(map.get("thisWeekOrderNumber")));
        row5.createCell(2).setCellValue("本周到诊数");
        row5.createCell(3).setCellValue(String.valueOf(map.get("thisWeekVisitsNumber")));

        XSSFRow row6 = sheet.createRow(7);
        row6.createCell(0).setCellValue("本月预约数");
        row6.createCell(1).setCellValue(String.valueOf(map.get("thisMonthOrderNumber")));
        row6.createCell(2).setCellValue("本月到诊数");
        row6.createCell(3).setCellValue(String.valueOf(map.get("thisMonthVisitsNumber")));


        XSSFRow row7 = sheet.createRow(9);
        row7.createCell(0).setCellValue("热门套餐");
        XSSFRow row8 = sheet.createRow(10);
        row8.createCell(0).setCellValue("套餐名称");
        row8.createCell(1).setCellValue("预约数量");
        row8.createCell(2).setCellValue("占比");
        row8.createCell(3).setCellValue("备注");


        ArrayList<Map> hotSetmeal = (ArrayList<Map>) map.get("hotSetmeal");

        int y=11;
        for (int i = 0; i < hotSetmeal.size(); i++) {
            XSSFRow row9 = sheet.createRow(y);
            row9.createCell(0).setCellValue(String.valueOf(hotSetmeal.get(i).get("name")));
            row9.createCell(1).setCellValue(String.valueOf(hotSetmeal.get(i).get("setmeal_count")));
            row9.createCell(2).setCellValue(String.valueOf(hotSetmeal.get(i).get("proportion")));
            row9.createCell(3).setCellValue(String.valueOf(hotSetmeal.get(i).get("remark")));
            y++;
        }

        //  写出去
        FileOutputStream fos = new FileOutputStream(new File("D:\\运营数据报表.xlsx"));
        workbook.write(fos);
        fos.flush();
        fos.close();
        workbook.close();
    }
}
